package com.yuanduan.mplus.view;

import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.cj.framework.app.BaseActivity;
import com.cj.framework.app.BaseApplication;
import com.cj.framework.util.IOkHttp;
import com.cj.framework.util.Msg;
import com.cj.framework.util.OkHttpImp;
import com.yuanduan.mplus.R;
import com.yuanduan.mplus.config.Config;
import com.yuanduan.mplus.db.dao.UserDao;
import com.yuanduan.mplus.db.entity.User;
import com.yuanduan.mplus.model.entity.RS;

import java.util.HashMap;
import java.util.List;

/**
 * 重置密码界面
 */
public class ChangePwdActivity extends BaseActivity implements View.OnClickListener {
    private ImageView iv_back;
    private TextView tv_insurechange;
    private EditText et_oldPwd;
    private EditText et_newPwd;
    private EditText et_insureNewPwd;

    private String oldPwd;
    private String newPwd;
    private String insureNewPwd;
    private User user;
    private UserDao userDao;
    private IOkHttp okHttp=new OkHttpImp();
    private IOkHttp.IHttpCallback callback=new IOkHttp.IHttpCallback() {
        @Override
        public void onResponse(Msg msg, int index) {
            String message=msg.content();
            String info="";
            RS rs= (RS) Config.decry(message,RS.class);
            switch (rs.getRs()){
                case -1:
                    info="非法访问";
                    break;
                case -2:
                    info="旧密码错误";
                    break;
                case -3:
                    info="新密码不一致";
                    break;
                case -4:
                    info="未知原因";
                    break;
                case 1:
                    info="修改成功";
                    user.setUserPwd(newPwd);
                    userDao.update(user);
                    break;
            }
            Toast.makeText(ChangePwdActivity.this,info,Toast.LENGTH_SHORT).show();
            if(rs.getRs()==1){
                ChangePwdActivity.this.finish();
            }
        }

        @Override
        public void onError(Exception e) {
            Toast.makeText(ChangePwdActivity.this,"网络出错",Toast.LENGTH_SHORT).show();
        }
    };

//    private IHttp iHttp=new HttpImp();
//    private IHttp.HttpInfoCallback callback=new IHttp.HttpInfoCallback() {
//        @Override
//        public void onSucceed(Object result) {
//            RS rs= (RS) Config.decry(result,RS.class);
//            if(rs.getRs()==1){
//                //更新本地数据库
//                user.setUserPwd(newPwd);
//                Toast.makeText(ChangePwdActivity.this,"修改成功",Toast.LENGTH_SHORT).show();
//                ChangePwdActivity.this.finish();
//            }else{
//                Toast.makeText(ChangePwdActivity.this,"修改失败",Toast.LENGTH_SHORT).show();
//            }
//        }
//
//        @Override
//        public void onFailed(Exception e) {
//            Toast.makeText(ChangePwdActivity.this,"网络请求失败",Toast.LENGTH_SHORT).show();
//        }
//    };
    @Override
    protected void findId() {
        iv_back= (ImageView) findViewById(R.id.changePwd_iv_back);
        tv_insurechange= (TextView) findViewById(R.id.changePwd_tv_insureChange);
        et_oldPwd= (EditText) findViewById(R.id.changePwd_et_oldPwd);
        et_newPwd= (EditText) findViewById(R.id.changePwd_et_newPwd);
        et_insureNewPwd= (EditText) findViewById(R.id.changePwd_et_insureNewPwd);
    }

    @Override
    protected void logic() {
        userDao= BaseApplication.getDaoSession().getUserDao();
        List list=userDao.queryBuilder().list();
        if(list.size()!=0){
            user= (User) list.get(0);
        }
        oldPwd=user.getUserPwd();
        iv_back.setOnClickListener(this);
        tv_insurechange.setOnClickListener(this);
    }

    @Override
    protected int setLayout() {
        return R.layout.activity_change_pwd;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.changePwd_iv_back:
                finish();
                break;
            case R.id.changePwd_tv_insureChange:
                if(!checkOldPwd()){
                    return;
                }
                if(!checkNewPwd()){
                    return;
                }
                //发送网络请求
                HashMap<String,String> hashMap=new HashMap<>();
                hashMap.put("UserPhone",user.getUserPhone());
                hashMap.put("UserPassword",user.getUserPwd());
                hashMap.put("OldPassword",user.getUserPwd());
                hashMap.put("NewPassword1",newPwd);
                hashMap.put("NewPassword2",insureNewPwd);
                okHttp.httpInfoPost(Config.URL+Config.CHANGEPWD,Config.translateMap(hashMap),callback);
                break;
        }
    }

    private boolean checkNewPwd() {
        //检查新密码和确认新密码是否一致
        //检查新密码和旧密码是否不一致
        newPwd=et_newPwd.getText().toString().trim();
        insureNewPwd=et_insureNewPwd.getText().toString().trim();
        if(!newPwd.equals(insureNewPwd)){
            Toast.makeText(this,"新密码不一致",Toast.LENGTH_SHORT).show();
            return false;
        }
        if(newPwd.equals(oldPwd)){
            Toast.makeText(this,"旧密码与新密码一致",Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }

    private boolean checkOldPwd() {
        if(oldPwd.equals(et_oldPwd.getText().toString().trim())){
            return true;
        }
        Toast.makeText(this,"旧密码错误",Toast.LENGTH_SHORT).show();
        return false;
    }

}
